package cn.stylefeng.guns.modular.business.service.impl;


import cn.stylefeng.guns.modular.business.common.BaseAsyncTask;
import cn.stylefeng.guns.modular.business.model.params.MessageDTO;
import cn.stylefeng.guns.modular.business.model.params.WxMessageDTO;
import cn.stylefeng.guns.modular.business.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import weixin.popular.bean.message.templatemessage.TemplateMessageItem;

import java.util.LinkedHashMap;

@Service
@Slf4j
public class MessageServiceImpl implements MessageService {


    private final BaseAsyncTask baseAsyncTask;

    @Value("${recruitment-notice-template.id}")
    private String recruitmentNoticeTemplateId;

    @Autowired
    public MessageServiceImpl(BaseAsyncTask baseAsyncTask) {
        this.baseAsyncTask = baseAsyncTask;
    }


    @Override
    public boolean sendTemplateMessage(MessageDTO messageDTO) {
        LinkedHashMap<String, TemplateMessageItem> data = new LinkedHashMap<>();
        WxMessageDTO wxMessageDTO = messageDTO.getData();
        data.put("first", new TemplateMessageItem(wxMessageDTO.getFirst(), null));
        data.put("remark", new TemplateMessageItem(wxMessageDTO.getRemark(), null));
        data.put("keyword1", new TemplateMessageItem(wxMessageDTO.getKeyword1(), null));
        data.put("keyword2", new TemplateMessageItem(wxMessageDTO.getKeyword2(), null));
        data.put("keyword3", new TemplateMessageItem(wxMessageDTO.getKeyword3(), null));
        data.put("keyword4", new TemplateMessageItem(wxMessageDTO.getKeyword4(), null));
        String templateId = null;
        switch (messageDTO.getMessageType()) {
            case RECRUITMENT_NOTICE:
                templateId = recruitmentNoticeTemplateId;
                break;
            default:
                break;
        }
        if (templateId == null) {
            return false;
        }
        baseAsyncTask.asyncSendTemplateMessage(messageDTO.getOpenidList(), templateId, data, wxMessageDTO.getUrl());
        return true;
    }
}
